package library.net.retrofit.request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import library.net.retrofit.RxContextType;
import okhttp3.RequestBody;

/**
 * request params by json
 *
 * @author Created by qiang.hou on 2018/11/22.
 * @version 1.0
 */
public class BaseJSONRequest {
    private JSONObject params = new JSONObject();

    public String getJson() {
        return params.toString();
    }

    public RequestBody getRequestBody() {
        return RequestBody.create(okhttp3.MediaType.parse(RxContextType.JSON_DATA), getJson());
    }

    public void put(String key, int value) {
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void put(String key, float value) {
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void put(String key, long value) {
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void put(String key, boolean value) {
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void put(String key, String value) {
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void put(String key, Object value) {
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void putIntArray(String key, List<Integer> values) {
        if (null == values || values.size() == 0) {
            return;
        }
        JSONArray array = new JSONArray();
        for (Integer i : values) {
            array.put(i);
        }
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void putFloatArray(String key, List<Float> values) {
        if (null == values || values.size() == 0) {
            return;
        }
        JSONArray array = new JSONArray();
        for (Float i : values) {
            array.put(i);
        }
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void putLongArray(String key, List<Long> values) {
        if (null == values || values.size() == 0) {
            return;
        }
        JSONArray array = new JSONArray();
        for (Long i : values) {
            array.put(i);
        }
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void putBooleanArray(String key, List<Boolean> values) {
        if (null == values || values.size() == 0) {
            return;
        }
        JSONArray array = new JSONArray();
        for (Boolean i : values) {
            array.put(i);
        }
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void putStringArray(String key, List<String> values) {
        if (null == values || values.size() == 0) {
            return;
        }
        JSONArray array = new JSONArray();
        for (String i : values) {
            array.put(i);
        }
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void putExtraArray(String key, List<Object> values) {
        if (null == values || values.size() == 0) {
            return;
        }
        JSONArray array = new JSONArray();
        for (Object i : values) {
            array.put(i);
        }
        try {
            if (params.has(key)) {
                params.remove(key);
            }
            params.put(key, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
